package com.wemei.auto.ui.dialog

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import com.wemei.auto.ui.R
import com.wemei.auto.ui.databinding.DialogAlertBinding
import com.wemei.auto.ui.ext.getString
import com.wemei.auto.ui.ext.gone
import com.wemei.auto.ui.ext.isLandscape

private typealias Callback = (() -> Unit)?

class AlertDialog(context: Context) : Dialog(context, R.style.dimLoadingdialog),
    View.OnClickListener {
    private lateinit var viewBinding: DialogAlertBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        viewBinding = DialogAlertBinding.inflate(LayoutInflater.from(context))
        setContentView(viewBinding.root)
        viewBinding.apply {
            (titleStr ?: R.string.title_dialog_tips.getString()).let { tvTitle.text = it }
            messageStr?.let { tvMessage.text = it }
            if (positiveButtonCallback != null && positiveButtonName.isNullOrEmpty()) positiveButtonName =
                R.string.button_confirm.getString()
            if (!positiveButtonName.isNullOrEmpty()) {
                tvPositiveButton.apply {
                    text = positiveButtonName
                    setOnClickListener(this@AlertDialog)
                }
            } else tvPositiveButton.gone()
            if (negativeButtonCallback != null && negativeButtonName.isNullOrEmpty()) negativeButtonName =
                R.string.button_cancel.getString()
            if (!negativeButtonName.isNullOrEmpty()) {
                tvNegativeButton.apply {
                    text = negativeButtonName
                    setOnClickListener(this@AlertDialog)
                }
            } else tvNegativeButton.gone()
            setCancelable(mCancelable)
            window?.attributes?.run {
                width = (context.resources.displayMetrics.widthPixels * (if (context.isLandscape) 0.5 else 0.75)).toInt()
            }
            ivClose.isVisible = mCancelable
            ivClose.setOnClickListener {
                dismiss()
                closeCallback?.invoke()
            }
        }
    }

    override fun onClick(v: View?) {
        when (v) {
            viewBinding.tvPositiveButton -> v.let {
                if (positiveDismissAfterClick) {
                    dismiss()
                }
                positiveButtonCallback?.invoke()
            }
            viewBinding.tvNegativeButton -> v.let {
                if (negativeDismissAfterClick) {
                    dismiss()
                }
                negativeButtonCallback?.invoke()
            }
        }
    }

    private var titleStr: CharSequence? = null
    private var messageStr: CharSequence? = null
    private var positiveButtonName: CharSequence? = null
    private var positiveButtonCallback: Callback = null
    private var positiveDismissAfterClick: Boolean = true
    private var negativeButtonName: CharSequence? = null
    private var negativeButtonCallback: Callback = null
    private var negativeDismissAfterClick: Boolean = true
    var closeCallback: Callback = null
    var mCancelable: Boolean = true


    fun title(title: CharSequence = "", titleRes: Int = 0) {
        titleStr = if (titleRes != 0) titleRes.getString() else title
    }

    fun message(message: CharSequence = "", messageRes: Int = 0) {
        messageStr = if (messageRes != 0) messageRes.getString() else message
    }

    fun positiveButton(
        name: CharSequence = "",
        nameRes: Int = 0,
        dismissAfterClick: Boolean = positiveDismissAfterClick,
        callback: Callback = null,
    ) {
        positiveButtonName = if (nameRes != 0) nameRes.getString() else name
        positiveDismissAfterClick = dismissAfterClick
        positiveButtonCallback = callback
    }

    fun negativeButton(
        name: CharSequence = "",
        nameRes: Int = 0,
        dismissAfterClick: Boolean = negativeDismissAfterClick,
        callback: Callback = null,
    ) {
        negativeButtonName = if (nameRes != 0) nameRes.getString() else name
        negativeDismissAfterClick = dismissAfterClick
        negativeButtonCallback = callback
    }
}

fun Activity?.showAlertDialog(settings: AlertDialog.() -> Unit) =
    this?.let { AlertDialog(this).also(settings).show() }

fun Fragment?.showAlertDialog(settings: AlertDialog.() -> Unit) =
    this?.let { activity?.let { AlertDialog(it).also(settings).show() } }